//
//  WWLoginController.swift
//  -00 旺旺微博
//
//  Created by 伍良旺 on 16/4/16.
//  Copyright © 2016年 伍良旺. All rights reserved.
//

import UIKit
import SVProgressHUD
/*
AppKey:3295966420
App Secret：1d3d91f610525889f404184f72ff7bec
http://www.baidu.com/
*/

class WWLoginController: UIViewController {

    lazy var webView:UIWebView = UIWebView()
    
    
    //更换主view 成为 webview
    override func loadView() {
        
        view = webView
        //设置代理
        webView.delegate = self
        
        //拼接路径
        let urlStr = "https://api.weibo.com/oauth2/authorize?client_id=\(AppKey)&redirect_uri=\(Redirect_URI)"
        let url = NSURL(string: urlStr)
        
        let req = NSURLRequest(URL: url!)
        
        webView.loadRequest(req)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        setupUI()
       
    }
//    https://api.weibo.com/oauth2/authorize
    
    
    
    func setupUI(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "clickNavLeftItemCantel")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "填充", style: UIBarButtonItemStyle.Plain, target: self, action: "clickNavRightItem")
        navigationItem.title = "微博登录"
    }

    @objc private func clickNavLeftItemCantel(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    
   @objc private func clickNavRightItem(){
        print("闹着玩的")
    
    let jsString = "document.getElementById('userId').value='\(wwWeiboName)';document.getElementById('passwd').value='\(wwWeiboPass)'"
    // JS注入
    webView.stringByEvaluatingJavaScriptFromString(jsString)

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

}
extension WWLoginController:UIWebViewDelegate{
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    //发送网络请求时调用 默认返回true 返回false 将不加载此网络强求
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        /*
        request.URL?.absoluteString 拿到网络请求的地址
        
        Optional("http://www.baidu.com/?code=ef31ba9fb1c5eb009a9b00916ab561ff")
        Optional("https://m.baidu.com/?code=ef31ba9fb1c5eb009a9b00916ab561ff&from=844b&vit=fps")
        */
        
        //判断url是否有值 且是不是回调页网网址开头的
        if let url = request.URL?.absoluteString  where url.hasPrefix(Redirect_URI){
            //拿到满足条件的url的参数
//            print(url)
            if let par = request.URL?.query {
//                print(par)
                //截取code= 后面的字符
                let code = par.substringFromIndex("code=".endIndex)
                print(code)
                
            }
        }
        
       // print(request.URL?.absoluteString)
        
        return true
    }
    
}

